package iphoneBackupScanner.parsing;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Infoparser {

	private final InputStream input;

	private final Map<String, String> elements = new TreeMap<String, String>();

	public Infoparser(final String fileName) throws IOException {
		this.input = new FileInputStream(fileName);

		validateInfo();

	}

	private void validateInfo() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(input,"UTF-8"));
		br.readLine();
		br.readLine();
		br.readLine();
		br.readLine();

		
		boolean b = true;

		while (b) {
			String key = br.readLine().trim();

			if (key.startsWith("<key>"))
				elements.put(removeTags(key), removeTags(br.readLine().trim()));

			else
				b = false;
		}

	}

	private String removeTags(String tagged) {

		return tagged.replaceAll("</*[a-zA-Z]+>", "");
	}
	
	public String getElement(String key){
		
		return elements.get(key);
	}

}
